package org.software.views;

import org.software.dao.StudentDataProvider;
import org.software.entity.Group;
import org.software.entity.Student;
import org.software.views.classes.ClassFrame;
import org.software.views.classes.ClassListFrame;

import org.software.views.group.GroupFrame;
import org.software.views.group.GroupListFrame;
import org.software.views.student.StudentFrame;
import org.software.views.student.StudentListFrame;
import org.software.dao.GroupDataProvider;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuFrame extends JFrame {
    public MenuFrame() {
        setTitle("课堂点名系统");
        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.CENTER));


        JMenuBar menuBar = new JMenuBar();
        JMenu classMenu = new JMenu("班级管理");
        JMenu groupMenu = new JMenu("小组管理");
        JMenu studentMenu = new JMenu("学生管理");
        JMenu courseMenu = new JMenu("课堂管理");
        JMenuItem addClassItem = new JMenuItem("添加班级");
        JMenuItem classListItem = new JMenuItem("班级列表");
        JMenuItem addGroupItem = new JMenuItem("添加小组");
        JMenuItem groupListItem = new JMenuItem("小组列表");
        JMenuItem addStudenItem = new JMenuItem("添加学生");
        JMenuItem studenListItem = new JMenuItem("学生列表");
        JMenuItem rollCallGroupItem = new JMenuItem("随机小组");
        JMenuItem rollCallStudentItem = new JMenuItem("随机学生");


        classMenu.add(addClassItem);
        classMenu.add(classListItem);
        groupMenu.add(addGroupItem);
        groupMenu.add(groupListItem);
        studentMenu.add(addStudenItem);
        studentMenu.add(studenListItem);
        courseMenu.add(rollCallGroupItem);
        courseMenu.add(rollCallStudentItem);


        menuBar.add(classMenu);
        menuBar.add(groupMenu);
        menuBar.add(studentMenu);
        menuBar.add(courseMenu);

        setJMenuBar(menuBar);
        setVisible(true);

        addClassItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new ClassFrame();
            }
        });
        classListItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new ClassListFrame();
            }
        });
        addGroupItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new GroupFrame();
            }
        });
        groupListItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new GroupListFrame();
            }
        });
        addStudenItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new StudentFrame();
            }
        });
        studenListItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new StudentListFrame();
            }
        });

        rollCallGroupItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Group randomGroup = GroupDataProvider.getRandomGroup();
                if (randomGroup != null) {
                    new CallFrame("随机小组: " + randomGroup.getGroupName());
                } else {
                    JOptionPane.showMessageDialog(null, "没有可用的小组！", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        rollCallStudentItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Student randomStudent = StudentDataProvider.getRandomStudent();
                if (randomStudent != null) {
                    new CallFrame("随机学生: " + randomStudent.getStudentName());
                } else {
                    JOptionPane.showMessageDialog(null, "没有可用的学生！", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });



    }
}
